package com.excellent.system.controller;

import com.excellent.common.annotation.Log;
import com.excellent.common.core.controller.BaseController;
import com.excellent.common.core.domain.AjaxResult;
import com.excellent.common.core.page.TableDataInfo;
import com.excellent.common.enums.BusinessType;
import com.excellent.common.utils.poi.ExcelUtil;
import com.excellent.system.domain.SysNews;
import com.excellent.system.domain.SysTaskStu;
import com.excellent.system.domain.SysWeekNews;
import com.excellent.system.service.ISysNewsService;
import com.excellent.system.service.ISysTaskStuService;
import com.excellent.system.service.ISysWeekNewsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Calendar;
import java.util.List;

/**
 * 周报管理Controller
 *
 * @author excellent
 * @date 2021-06-30
 */
@Controller
@RequestMapping("/system/adminnews")
public class SysNewsController extends BaseController
{
    private String prefix = "system/adminnews";

    @Autowired
    private ISysNewsService sysNewsService;

    @Autowired
    private ISysTaskStuService sysTaskStuService; //引入学生学期

    @Autowired
    private ISysWeekNewsService sysWeekNewsService; //引入具体周报

    @GetMapping()
    public String adminnews()
    {
        return prefix + "/adminnews";
    }

    /**
     * 查询周报管理列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysNews sysNews)
    {
        startPage();
        List<SysNews> list = sysNewsService.selectSysNewsList(sysNews);
        return getDataTable(list);
    }

    /**
     * 导出周报管理列表
     */
    @Log(title = "周报管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysNews sysNews)
    {
        List<SysNews> list = sysNewsService.selectSysNewsList(sysNews);
        ExcelUtil<SysNews> util = new ExcelUtil<SysNews>(SysNews.class);
        return util.exportExcel(list, "adminnews");
    }

    /**
     * 新增周报管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存周报管理
     */
    @Log(title = "周报管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysNews sysNews)
    {
        return toAjax(sysNewsService.insertSysNews(sysNews));
    }

    /**
     * 修改周报管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysNews sysNews = sysNewsService.selectSysNewsById(id);
        mmap.put("sysNews", sysNews);
        return prefix + "/edit";
    }

    /**
     * 修改保存周报管理
     */
    @Log(title = "周报管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysNews sysNews)
    {
        return toAjax(sysNewsService.updateSysNews(sysNews));
    }

    /**
     * 删除周报管理
     */
    @Log(title = "周报管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysNewsService.deleteSysNewsByIds(ids));
    }

    @PostMapping( "/generatenew")
    @ResponseBody
    public AjaxResult generatenew()
    {
        Calendar c = Calendar.getInstance();  //获取系统当前年份
        SysNews s = new SysNews();
        List<SysNews> sysNews = sysNewsService.selectSysNewsList(s);//获取所有管理周报列表
        SysTaskStu s1 = new SysTaskStu();
        int month = c.get(Calendar.MONTH) + 1;//本函数中月份需要加一
        int year = c.get(Calendar.YEAR);
        if (month >= 1 && month <= 6) {
            s1.setTaskTerm((year - 1) + "学年第2学期");
        } else {
            s1.setTaskTerm(year + "学年第1学期");
        }
        List<SysTaskStu> sysTaskStus = sysTaskStuService.selectSysTaskStuList(s1); //获取当前学期所有的学生课题环节

        for (SysTaskStu sys: sysTaskStus   //遍历所有的学生课题
             ) {
            String name = sys.getTaskClass();  //获取所有的课表环节字段
            if(name.equals("实践成果一")){
                for (SysNews sys1: sysNews //遍历所有的周报列表
                     ) {
                    String name1 = sys1.getTask();  //获取的环节字段
                    if(name1.equals("实践成果一")){
                        SysWeekNews week1 = new SysWeekNews();
                        week1.setStuId(sys.getStuId());  //将学生课表表中的学生学号插入
                        week1.setTeaId(sys.getTeaId());  //将学生课表中的学生学号插入
                        week1.setStuName(sys.getStuName());
                        week1.setTask(sys.getTaskClass()); //将学生课表中的实践环节类型插入
                        week1.setNewNum(sys1.getNum());  //将周报表中的次数插入
                        week1.setStatus(0);
                        week1.setDeadTime(sys1.getDeadtime());  //将周报表中的截止日期插入
                        year = c.get(Calendar.YEAR);
                        if (month >= 1 && month <= 6) {
                            week1.setTaskTerm((year - 1) + "学年第2学期");
                        } else {
                            week1.setTaskTerm((year) + "学年第1学期");
                        }
                        sysWeekNewsService.insertSysWeekNews(week1);//将数据插入
                    }
                }
            }
            if (name.equals("实践成果二")){
                for (SysNews sys1: sysNews //遍历所有的周报列表
                ) {
                    String name1 = sys1.getTask();  //获取的环节字段
                    if(name1.equals("实践成果二")){
                        SysWeekNews week1 = new SysWeekNews();
                        week1.setStuId(sys.getStuId());  //将学生课表表中的学生学号插入
                        week1.setTeaId(sys.getTeaId());  //将学生课表中的学生学号插入
                        week1.setStuName(sys.getStuName());
                        week1.setTask(sys.getTaskClass()); //将学生课表中的实践环节类型插入
                        week1.setNewNum(sys1.getNum());  //将周报表中的次数插入
                        week1.setStatus(0);
                        week1.setDeadTime(sys1.getDeadtime());  //将周报表中的截止日期插入
                        year = c.get(Calendar.YEAR);
                        if (month >= 1 && month <= 6) {
                            week1.setTaskTerm((year - 1) + "学年第2学期");
                        } else {
                            week1.setTaskTerm((year) + "学年第1学期");
                        }
                        sysWeekNewsService.insertSysWeekNews(week1);//将数据插入
                    }
                }
            }
        }
        return toAjax(true);
    }
}
